#!/usr/bin/env python3
# Copyright 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2025, Shanghai Iluvatar CoreX Semiconductor Co., Ltd.
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of NVIDIA CORPORATION nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import sys 
import argparse
import numpy as np
import pickle
import tempfile

import tritonclient.http as httpclient
from tritonclientutils import np_to_triton_dtype

from utils import *

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-u',
                        '--url',
                        type=str,
                        required=False,
                        default='localhost:8000',
                        help='Inference server URL. Default is localhost:8000.')
    parser.add_argument('--model-path',
                        type=str,
                        required=True,
                        default='model.so',
                        help='model path')
    parser.add_argument('--model-name',
                        type=str,
                        required=False,
                        default='model',
                        help='model name')
    parser.add_argument('--input-name',
                        type=str,
                        required=False,
                        default='input',
                        help='model input name')
    parser.add_argument('--output-name',
                        type=str,
                        required=False,
                        default='output',
                        help='model input name')
    parser.add_argument('--batch-size',
                        type=int,
                        required=False,
                        default=16,
                        help='model input batch size')
    parser.add_argument('--data-path',
                        type=str,
                        required=True,
                        default='./data',
                        help='model input data path.')
    
    FLAGS = parser.parse_args()


    input_shape = [FLAGS.batch_size, 3, 224, 224]
    
    # prepare model input data.
    input_path = os.path.join(tempfile.gettempdir(), 'resnet50_input.pkl')
    if not os.path.isfile(input_path):
        print("gen input data...")
        image_lists = os.listdir(FLAGS.data_path)
        images = []
        for path in image_lists[:batch_size]:
            path = os.path.join(FLAGS.data_path, path)
            image = image_load(path)
            images.append(image)
        
        images = np.array(images)
        with open(input_path, "wb") as f:
            pickle.dump(images, f)
    else:
        with open(input_path, "rb") as f:
            images = pickle.load(f)
    
    # igie inference result as baseline.
    desired_result_path = os.path.join(tempfile.gettempdir(), 'resnet50_result.pkl')
    if not os.path.exists(desired_result_path):
        # infer with igie python API
        import tvm 
        from tvm.contrib import graph_executor
        target = tvm.target.iluvatar(model="MR", options="-libs=cudnn,cublas,ixinfer")
        device = tvm.device(target.kind.name, 0)

        lib = tvm.runtime.load_module(FLAGS.model_path)
        module = graph_executor.GraphModule(lib["default"](device))
        module.set_input(FLAGS.input_name, tvm.nd.array(images, device))
        module.run()
        device.sync()
        
        desired_result = module.get_output(0).asnumpy()
        with open(desired_result_path, "wb") as f:
            pickle.dump(desired_result, f)
    else:
        with open(desired_result_path, "rb") as f:
            desired_result = pickle.load(f)
            
    print("# Part of desired result:")
    desired_result = desired_result.flatten()
    print(desired_result[:20])

    # get TIS Server inference result by sending request to it.
    try:
        concurrent_request_count = 5
        triton_client = httpclient.InferenceServerClient(
            url=FLAGS.url, concurrency=concurrent_request_count)
    except Exception as e:
        print("channel creation failed: " + str(e))
        sys.exit(1)
    
    print('Sending request to batching model ...')
    inputs = [ httpclient.InferInput(FLAGS.input_name, input_shape, "FP32")]
    inputs[0].set_data_from_numpy(images)
    
    print("Client is infering")
    response = triton_client.infer(FLAGS.model_name, inputs)

    result_meta = response.get_response()
    print("get result from {}, with version {}, total {} outputs".format(result_meta['model_name'],
                                                                         result_meta['model_version'],
                                                                         len(result_meta['outputs'])))
    actual_result = response.as_numpy(FLAGS.output_name)
    print("# Part of actual result:")
    actual_result = actual_result.flatten()
    print(actual_result[:20])
    
    # checking accuracy.
    print("# All close Asserting:")
    np.testing.assert_allclose(actual_result, desired_result, rtol=1e-1, atol=0, verbose=True)
    print('PASS!')
    sys.exit(0)
